package com.ruoyi.controller;

import java.util.List;

import com.ruoyi.common.utils.uuid.UUID;
import com.ruoyi.service.ICommodityService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.domain.SaleRecord;
import com.ruoyi.service.ISaleRecordService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 出售记录Controller --数据筛选
 *
 * @author ruoyi
 * @date 2023-08-08
 */
@Controller
@RequestMapping("/system/record")
public class SaleRecordController extends BaseController
{
    private String prefix = "system/record";

    @Autowired
    private ISaleRecordService saleRecordService;
    @Autowired
    private ICommodityService commodityService;
    @RequiresPermissions("system:record:view")
    @GetMapping()
    public String record()
    {
        return prefix + "/record";
    }

    /**
     * 查询出售记录列表
     */
    @RequiresPermissions("system:record:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(SaleRecord saleRecord)
    {
        startPage();
        List<SaleRecord> list = saleRecordService.selectSaleRecordList(saleRecord);
        return getDataTable(list);
    }

    /**
     * 导出出售记录列表
     */
    @RequiresPermissions("system:record:export")
    @Log(title = "出售记录", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(SaleRecord saleRecord)
    {
        List<SaleRecord> list = saleRecordService.selectSaleRecordList(saleRecord);
        ExcelUtil<SaleRecord> util = new ExcelUtil<SaleRecord>(SaleRecord.class);
        return util.exportExcel(list, "出售记录数据");
    }

    /**
     * 新增出售记录
     */
    @GetMapping("/add")
    public String add(ModelMap mmodel)
    {
        mmodel.put("commodityList",commodityService.list());
        return prefix + "/add";
    }

    /**
     * 新增保存出售记录
     */
    @RequiresPermissions("system:record:add")
    @Log(title = "出售记录", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(SaleRecord saleRecord)
    {

        return toAjax(saleRecordService.insertSaleRecord(saleRecord));
    }

    /**
     * 修改出售记录
     */
    @RequiresPermissions("system:record:edit")
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        SaleRecord saleRecord = saleRecordService.selectSaleRecordById(id);
        mmap.put("saleRecord", saleRecord);
        return prefix + "/edit";
    }

    /**
     * 修改保存出售记录
     */
    @RequiresPermissions("system:record:edit")
    @Log(title = "出售记录", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(SaleRecord saleRecord)
    {
        return toAjax(saleRecordService.updateSaleRecord(saleRecord));
    }

    /**
     * 删除出售记录
     */
    @RequiresPermissions("system:record:remove")
    @Log(title = "出售记录", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(saleRecordService.deleteSaleRecordByIds(ids));
    }
}
